package com.ycdr.leetcode.queue.example;

/**
 * 环形队列
 * 基于数组实现的
 * @date 2020年06月02日
 * @author dingguangyi
 */
public class ArrayCircleQueue {

    private int maxSize;
    private int rear;
    private int front;
    private int nItems;
    private Object[] queue;

    public ArrayCircleQueue(int i){
        this.maxSize=i;
        this.front=0;
        this.rear=-1;
        this.nItems=0;
        this.queue=new Object[i];
    }

    public void insert(Object o){
        if (rear==maxSize-1){
            rear=-1;
        }
        this.queue[++rear]=o;
        nItems++;
    }

    public Object remove(){
        Object temp=this.queue[front++];
        if (front==maxSize){
            front=0;
        }
        nItems--;
        return temp;
    }

    public Object peek(){
        return this.queue[front];
    }

    public boolean isFull(){
        return nItems==maxSize;
    }

    public boolean isEmpty(){
        return nItems==0;
    }

    public static void main(String[] args) {
        ArrayCircleQueue queue=new ArrayCircleQueue(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
        queue.insert(10);
    }
}
